$(function() {
    var paying = {
        send: function() {
            function standardPost(url, args) {
                var form = $("<form id='postForm' method='post' name='pay'></form>");
                form.attr({"action":url});
                for (arg in args){
                    if(null != args[arg] && args[arg].length != 0) {
                        var input = $("<input type='hidden'>");
                        input.attr({"name":arg});
                        input.val(args[arg]);
                        form.append(input);
                    }
                }
                // console.log(form.serialize());
                // $('.formWrap').html(form);
                // console.log(form.html())
                // form.submit();
                // form.remove();
                form.append('<input type="button" id="postSubmit" name="postInput">');
                $('body').append(form);

                $('#postSubmit').on('click', function(event) {
                    event.preventDefault();
                    form.submit();
                }).trigger('click');
                form.submit(function(event) {
                    event.preventDefault();
                });
            }

            $.ajax({
                url: wsCommon.host + '/api/orders/orderPay',
                dataType: wsCommon.jsonType,
                data: {
                    orderId: wsCommon.getUrlParam('id'),
                    returnUrl: wsCommon.host + '/pay-success.html?orderId=' + wsCommon.getUrlParam('id'),
                    frontFailUrl: wsCommon.host + '/cart3.html'
                },
                success: function(res) {
                    if(res.result) {
                        if (res.code == 1) {
                            if(res.data.payment == 1000) { //微信
                                $('.now').html('微信支付');
                                $('[data-role="tips"]').hide();
                                $('.paying-orderInfo').show();
                                $('.paying-main').show();
                                $('[data-role="orderid"]').html(wsCommon.getUrlParam('id'));
                                $('[data-role="ordername"]').html(res.data.subject);
                                $('[data-role="payer"]').html(res.data.sellerName);
                                $('.paying-orderInfo__money strong').html(res.data.curMoney.toFixed(2))
                                var qrcode = res.data.param;
                                $('.paying-main img').attr('src', qrcode);

                                setInterval(function() {
                                    paying.checkorder(wsCommon.getUrlParam('id'));
                                }, 10000);
                            } else {
                                standardPost(res.data.payUrl, res.data.param);
                            }
                        } else if(res.code == 1005) {
                            window.location.href = './member-orderdetail.html?orderId=' + wsCommon.getUrlParam('id');
                        } else {
                            $('.data-role="tips"').html(res.reason);
                        }
                    } else {
                        wsCommon.setCookie('go_back_link', window.location.href);
                        window.location.href = './passport-login.html';
                    }
                },
                error: function() {
                    // console.log('支付接口返回失败');
                }
            })
        },
        checkorder: function(id) {
            $.ajax({
                url: wsCommon.host + '/api/orders/queryByOrderId',
                dataType: wsCommon.jsonType,
                data: {
                    orderId:id,
                    isShowDetail: false,
                    isShowLog: false,
                    isShowCoupon: false
                },
                success: function(res) {
                    if(res.code == 1) {
                        if(res.result) {
                            if(res.data.orders.payStatus == 1) {
                                window.location.href = './pay-success.html?orderId=' + id;
                            }
                        } else {
                            wsCommon.setCookie('go_back_link', window.location.href);
                            window.location.href = './passport-login.html';
                        }
                    }
                },
                error: function() {}
            })
        },
        init: function() {
            this.send();
        }
    }

    paying.init();
})
